<?php
namespace Taxx\KernelBundle\Model\FormBuilder;
/**
 * Custom FormBuilder Class
 *
 * @author flocki
 */
class FormBuilder 
{
    protected $legend;
    protected $elements;
    
    public function __construct() 
    {
        $this->elements = array();
        $this->legend = '';
    }
    
    public function setLegend($legend_title)
    {
        $this->legend = '
            <legend>
                '.$legend_title.'
            </legend>
        ';
        return $this;
    }

    public function addHiddenField($id, $label, array $attributes=null)
    {
        $attr = $this->parseAttributes($attributes);     
        
        $this->elements[$id] = '<label for="'.$id.'">
            '.$label.'
            <input id="'.$id.'" type="hidden" '.$attr.'/>
            </label>';
        return $this;
    }
    
    public function addSelectField($id, $label, array $options, $selected_index=null, array $attributes=null)
    {
        $attr = $this->parseAttributes($attributes);
        
        $opt = '';
        foreach($options as $key=>$value)
        {
            $selected = '';
            if($key == $selected_index)
                $selected = ' selected="selected" ';
            
            $opt .= '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
        }
            
        $this->elements[$id] = '<label for="'.$id.'">
            '.$label.'
            <select id="'.$id.'" '.$attr.'>
                '.$opt.'
            </select>
            </label>'.
           '<ul id="'.$id.'_error_msg"></ul>';
        
        return $this;
    }
    
    public function addInputField($id, $label, array $attributes=null)
    {
        $attr = $this->parseAttributes($attributes);
        
        $this->elements[$id] =  '<label for="'.$id.'">
            '.$label.'
            <input id="'.$id.'" '.$attr.'/>
            </label>'.
           '<ul id="'.$id.'_error_msg"></ul>';
        
        return $this;
    }
    
    public function addUploadField($id, $label, array $attributes=null)
    {
        $attr = $this->parseAttributes($attributes);
        
        $this->elements[$id] =  '<label for="'.$id.'">
            '.$label.'
            <input id="'.$id.'" type="file" '.$attr.'/>
            </label>'.
           '<ul id="'.$id.'_error_msg"></ul>';
        
        
        return $this;
    }
    
    public function addCheckbox($id, $label, array $attributes=null)
    {
        $attr = $this->parseAttributes($attributes);
        
        $this->elements[$id] =  '<label for="'.$id.'">
            <input id="'.$id.'" type="checkbox" '.$attr.'/>
            '.$label.'
            </label>'.
           '<ul id="'.$id.'_error_msg"></ul>';
        
        return $this;
    }
    
    public function addButton($id, $value, array $attributes=null)
    {
        $attr = $this->parseAttributes($attributes);
        
        $this->elements[$id] =  '<button id="'.$id.'" '.$attr.'>'.$value.'</button>';
        
        return $this;
    }
    
    public function getForm($id, array $attributes=null)
    {
        $attr = $this->parseAttributes($attributes);
        
        $element_string = "";
        foreach($this->elements as $e)
            $element_string .= $e;
            
        $form = '
            <form id="'.$id.'" '.$attr.'>
            <fieldset>
            '.$this->legend.'
            '.$element_string.'
            </fieldset>
            </form>
        ';
        
        return $form; 
    }
    
    private function parseAttributes(array $attributes=null)
    {
        $attr = "";
        if(count($attributes)>0)
            foreach($attributes as $key=>$value)
                $attr .= ' '.$key.'="'.$value.'" ';
        
        return $attr;
    }   
}

?>
